package org.project.s.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.project.pojo.Detailimgs;
import org.project.pojo.Detailimgs0;
import org.project.pojo.Fengye;
import org.project.pojo.Img0;
import org.project.pojo.Imgs;
import org.project.pojo.Record0;
import org.project.pojo.Record1;
import org.project.service.user.ImgsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/banner2")
public class Bannercontroller {
@Resource
private ImgsService imgsService;

@RequestMapping("/selectban")
public String selectbanner2(){
	HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	String path=getLoaclURL(req)+"shell//";
	List<Record1> Record1list=new ArrayList<Record1>();
	List<Imgs> list=imgsService.selectbannerimgs();
	if(list!=null){
		Iterator<Imgs> it = list.iterator();
		while(it.hasNext()) {
			Imgs imgs=(Imgs)it.next();
			Record1 record1=new Record1();
			record1.setUrl(path+imgs.getImgName());
			record1.setId(imgs.getId());
			Record1list.add(record1);
		}
	}
	
	req.setAttribute("Record1list", Record1list);
    	return "showbanner";
}

@RequestMapping("/selecteight")
public String selecteight2(){
	HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	String path=getLoaclURL(req)+"shell//";
	List<Record1> Record1list=new ArrayList<Record1>();
   	
	List<Record0> list=imgsService.selecteightimgs();
	
	if(list!=null){
	
	Iterator<Record0> it = list.iterator();
	int id=0;
	boolean flag=false;
	while(it.hasNext()) {
		
		Record0 record0= (Record0) it.next();
		Record1 record1=new Record1();
		record1.setFunctionNum(record0.getFunctionNum());
		record1.setUrl(path+record0.getImgName());
		record1.setFunctionName(record0.getFunctionName());
		record1.setEnglishName(record0.getEnglishName());
		record1.setId(id);
		record1.setFlag(flag);
		Record1list.add(record1);
		id++;
	}
	
	}
	  req.setAttribute("Record1list", Record1list);
	  
	  return "showeight";
}


@RequestMapping("/selectfdimgs")
public String selectfdimgs(HttpServletRequest request, HttpServletResponse response){
	HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	String path=getLoaclURL(req)+"shell//";
	
	String str=request.getParameter("n");   
//	System.out.println(str);
	Fengye fengye=new Fengye();
     //总行数  
     int lineCount=imgsService.fdimgscount();  
     //每页的行数  
     int pageSize=6;  
     //总共的页数  
     int pageCount=(int)Math.ceil(lineCount*1.0/pageSize);  
       
     int n = 0;  
               
     if(str == null || str.equals("")){  
         n = 0;  
     }else if(str.equals("a")){  
    	 
          n = (Integer)request.getSession().getAttribute("pageNow");  
         n=n-pageSize;  
     }else if(str.equals("b")){  
          n = (Integer)request.getSession().getAttribute("pageNow");  
         n=n+pageSize;  
     }else if(str.equals("0")){  
         n=0;  
     }else{  
         n=(pageCount-1)*pageSize;  
     }     
       
     if(n>((pageCount-1)*pageSize)){  
         n=(pageCount-1)*pageSize;  
     }  
     if(n<0){  
         n=0;  
     }     
       
     request.getSession().setAttribute("pageNow", n);  
     request.getSession().setAttribute("pageCount", pageCount);    
       
   
  /*   System.out.println(n);
     System.out.println(pageSize);*/
     fengye.setN(n);
     fengye.setPageSize(pageSize);
     List<Detailimgs0> list= imgsService.selectfdimgs(fengye);
    
     List<Detailimgs> list2=new ArrayList<Detailimgs>();
     System.out.println(list2);
	 if(list!=null){
		
		 Iterator<Detailimgs0> it=list.iterator();
			while(it.hasNext()) {
				Detailimgs0 detailimgs0=it.next();
				Detailimgs detailimgs=new Detailimgs();
				detailimgs.setFunctionNum(detailimgs0.getFunctionNum());
				detailimgs.setId(detailimgs0.getId());
				detailimgs.setUrl(path+detailimgs0.getImgName());
				list2.add(detailimgs);
			}
	 }
	       req.setAttribute("list2", list2);
	
	return "showfdimgs";
	
}



//增加明细图片
@RequestMapping("/addfdimgs")
public String addfdimgs(@Param("functionNum")String functionNum,@ RequestParam MultipartFile items_pic){
	 Img0 img0=new Img0();
	HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	String originalFilename = items_pic.getOriginalFilename();
	
	if (items_pic!=null && originalFilename!=null && originalFilename.length()>0) {
	String pic_path=getRealPath(req)+"shell//";
	//新图片名称
	String newFileName=UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
//	System.out.println(newFileName);
	 System.out.println(items_pic.getOriginalFilename());
	 File newFile=new File(pic_path+newFileName); 
	 img0.setFunctionNum(functionNum);
	 img0.setImgName(newFileName);
	 
	 //开始读图片
	 try {
			items_pic.transferTo(newFile);
			
			
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			 
			
			}
	     int i= imgsService.addfdimgs(img0);
	  
	     
	     if(i>0){
		   return "redirect:/banner2/selectfdimgs";
		   
	   }else{
		   return "";
	   }
	
}








@RequestMapping("/updatefdimgs")
public String updatefdimgs(@Param("id")String id,@ RequestParam MultipartFile items_pic){
	    System.out.println(id);
	   int id0= Integer.valueOf(id);
	    Img0 img0=new Img0();
		HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		String originalFilename = items_pic.getOriginalFilename();
		
		if (items_pic!=null && originalFilename!=null && originalFilename.length()>0) {
		String pic_path=getRealPath(req)+"shell//";
		//新图片名称
		String newFileName=UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
//		System.out.println(newFileName);
//		 System.out.println(items_pic.getOriginalFilename());
		 File newFile=new File(pic_path+newFileName); 
	    
		 img0.setId(id0);
		 img0.setImgName(newFileName);
		 
		  
		 //开始读图片
		 try {
				items_pic.transferTo(newFile);
				
				
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				 
				
				}
		     int i= imgsService.updatefdimgs(img0);
		  
		     
		     if(i>0){
			   return "redirect:/banner2/selectfdimgs";
			   
		   }else{
			   return "";
		   }
		 
}


@RequestMapping("/deletefdimgs")
public String deletefdimgs(@Param("id")int id){
	Img0 img0=new Img0();
	img0.setId(id);
	int i=imgsService.deletefdimgs(img0);
	
	if(i>0){
		return "redirect:/banner2/selectfdimgs";
	}else{
		return "";
	}
	
	
	
	
}



@RequestMapping("/updateeightimgs")
public String updateeightimgs(@Param("functionNum")String functionNum,@ RequestParam MultipartFile items_pic){
	    Img0 img0=new Img0();
		HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		String originalFilename = items_pic.getOriginalFilename();
		
		if (items_pic!=null && originalFilename!=null && originalFilename.length()>0) {
		String pic_path=getRealPath(req)+"shell//";
		//新图片名称
		String newFileName=UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
//		System.out.println(newFileName);
	//	 System.out.println(items_pic.getOriginalFilename());
		 File newFile=new File(pic_path+newFileName); 
	     
		 img0.setFunctionNum(functionNum);
		 img0.setImgName(newFileName);
		 
		//开始读图片
		 try {
				items_pic.transferTo(newFile);
				
				
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				 
				
				}
		 
		 int i=imgsService.updateeightimgs(img0);
		 if(i>0){
			   return "redirect:/banner2/selecteight";
			   
		   }else{
			     return "";
		   }
		 
	
	
}











@RequestMapping("/updatebanner")
public String updatebanner(@Param("id")int id,@ RequestParam MultipartFile items_pic){
	System.out.println(id);
	 Img0 img0=new Img0();
		HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		String originalFilename = items_pic.getOriginalFilename();
		
		if (items_pic!=null && originalFilename!=null && originalFilename.length()>0) {
		String pic_path=getRealPath(req)+"shell//";
		//新图片名称
		String newFileName=UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
//		System.out.println(newFileName);
//		 System.out.println(items_pic.getOriginalFilename());
		 File newFile=new File(pic_path+newFileName); 
		 img0.setId(id);
		 img0.setImgName(newFileName);
		 

		
		//开始读图片
		 try {
				items_pic.transferTo(newFile);
				
				
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				 
				
				}
		
		     int i= imgsService.updatebanner(img0);
		    
		     if(i>0){
				   return "mainfra";
				   
			   }else{
				     return "";
			   }
		
		
		}



/**
 * @方法名:获取服务器盘符路径
 * @参数:
 * @输出:
 * @备注:
 * @作者: 徐磊
 * @时间: 2017年7月5 11:42:57
 * @修改:
 */

public static String getRealPath(HttpServletRequest request) {

	return request.getSession().getServletContext().getRealPath("/");
}







public static String getLoaclURL(HttpServletRequest request) {
	
	String path_now = request.getContextPath();
	return request.getScheme() + "://" + request.getServerName() + ":"
			+ request.getServerPort() + path_now + "/";
}
}
